package com.yt.fras.handler;

import com.alibaba.fastjson.JSON;
import com.yt.fras.constant.ResultCode;
import com.yt.fras.pojo.base.JsonResult;
import com.yt.fras.utils.ResultTool;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName:CustomizeAccessDeniedHandler
 * Package:com.yt.fras.handler
 * Description:
 * 权限拒绝处理逻辑
 *
 * @date:2021/8/17 16:53
 * @author:yt
 */

@Component
public class CustomizeAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
        JsonResult result = ResultTool.fail(ResultCode.NO_PERMISSION);
        httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(result));
    }
}